package guoanmaker.operator.business.dao;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import guoanmaker.operator.business.model.PersonalUser;
import guoanmaker.operator.business.model.ProvideArea;
import guoanmaker.operator.business.model.ProvideCity;
import guoanmaker.operator.business.model.ProvideCommodity;
import guoanmaker.operator.business.model.ProvideOrderForm;
import guoanmaker.operator.business.modelnobind.OderFormRo;
import guoanmaker.operator.business.modelnobind.OperatorBusinessRo;
import guoanmaker.operator.business.modelnobind.OperatorUserRo;
import guoanmaker.operator.business.repository.PersonalUserRepository;
import guoanmaker.operator.business.repository.ProvideAreaRepository;
import guoanmaker.operator.business.repository.ProvideCityRepository;
import guoanmaker.operator.business.repository.ProvideCommodityRepository;
import guoanmaker.operator.business.repository.ProvideOrderFormRepository;
import guoanmaker.operator.business.repository.ProviderBusinessRepository;


@Repository
public class OperatorUserInfoDao {

	@PersistenceContext
	EntityManager em;
	
	@Autowired
	private  ProviderBusinessRepository providerBusinessRepository;
	
	@Autowired
	private PersonalUserRepository  personalUserRepository;
	
	@Autowired
	private  ProvideCommodityRepository provideCommodityRepository;
	
	@Autowired
	private ProvideOrderFormRepository provideOrderFormRepository;
	

	@Autowired
	private ProvideAreaRepository  provideAreaRepository;
	
	@Autowired
	private ProvideCityRepository  provideCityRepository;
	public Map<String,Object>   selectUserinfosPage(Integer page,Integer size){
		String jpql="select new guoanmaker.operator.business.modelnobind.OperatorUserRo(id,username,createTime,"
				+ "isProvider) from PersonalUser u";
		
		Query query = em.createQuery(jpql);
		List<OperatorUserRo> result = query.getResultList();
		Map<String,Object>  map=new HashMap<>();
		map.put("totle", result.size());
		query.setFirstResult(page*size);
		query.setMaxResults(size);
		List<OperatorUserRo> result1 = query.getResultList();
		map.put("result", result1);
		return  map;
	};
	public List<OperatorUserRo>   selectUserinfos(){
		String jpql="select new guoanmaker.operator.business.modelnobind.OperatorUserRo(id,username,createTime,"
				+ "isProvider) from PersonalUser u";
		Query query1 = em.createQuery(jpql);
		List<OperatorUserRo> listI =query1.getResultList();
		return  listI;
	};
	
	/**
	 * 
	 * @param page                        默认为0
	 * @param size                        每页10条
	 * @param acount                      商户账号
	 * @param status                      状态
	 * @param volume                      成交量
	 * @param volumesymbol                成交量符号
	 * @param clinchadealAmount           成交金额
	 * @param clinchadealAmountsymbol     成交金额符号
	 * @param closing                     成交率
	 * @param closingsymbol               成交率符号
	 * @param certification               是否认证
	 * @param registStartDate             注册时间（起始时间）
	 * @param registEndDate               注册时间(结束时间）
	 * @return
	 */
   public Map<String,Object>  selectUser(Integer page,Integer size,String acount,String status,Integer volume, String volumesymbol, 
           Double clinchadealAmount,String clinchadealAmountsymbol,Double closing, String closingsymbol, String certification,String registStartDate,
           String registEndDate){
	   String jpql="";
	   OperatorUserRo  operatorUserRo=null;
	   List<OperatorUserRo>  lists=new ArrayList<>();
	   jpql="select new guoanmaker.operator.business.modelnobind.OperatorUserRo(id,username,createTime,"
				+ "isProvider) from PersonalUser u";
			if(acount!=null&&!acount.equals("")){
				 jpql="and u.username like '%'"+acount+"'%'";
			}else if(status!=null&&!status.equals("")&&status.equals("正常")){
			     jpql="and u.isProvider!=4";
			}else if(status!=null&&!status.equals("")&&status.equals("冻结")){
				 jpql="and u.isProvider=4";
			}else if(registStartDate!=null&&registEndDate!=null){
				SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
				Date date = null;
				Date date1=null;
				try {
					date = sdf.parse(registStartDate);
					 date1 = sdf.parse(registEndDate);
				} catch (ParseException e) {
					e.printStackTrace();
				}
				jpql="and u.createDate between '"+date+"'and'"+date1+"'";
			}
			Query query = em.createQuery(jpql);
			List<OperatorUserRo> result = query.getResultList();
			Map<String,Object>  map=new HashMap<>();
			map.put("totle", result.size());
			query.setFirstResult(page*size);
			query.setMaxResults(size);
			List<OperatorUserRo> listI = query.getResultList();
			if(listI.size()>0){
				 for (int i = 0; i < listI.size(); i++) {
					 operatorUserRo=listI.get(i);
					 PersonalUser  personalUser=	personalUserRepository.findOne(operatorUserRo.getId());
						if(personalUser!=null){
							if(personalUser.getIsProvider().equals("2")){
								operatorUserRo.setAssociatCompany("已认证");
							}else{
								operatorUserRo.setAssociatCompany("未认证");
							}
							//交易金额
							List<ProvideCommodity>  prcs = provideCommodityRepository.selectByCommodityUser(operatorUserRo.getId());
							Double sumPrice=0.0;//交易金额
							int volume1=0;//成交量
							int sumvolume=0; //生成订单总数
							for (ProvideCommodity provideCommodity : prcs) {
							  Double shangpinPrice=0.0;	
							  int volumeuniq=0;
							  int vo=0;
							  List<ProvideOrderForm> pofs=  provideOrderFormRepository.selectBycommodity(provideCommodity.getId());
							  if(pofs.size()>0){
								  shangpinPrice=provideOrderFormRepository.selectSumrRate(provideCommodity.getId());
								  if(shangpinPrice==0.0){
									  shangpinPrice=0.0;
								  }
							  }
						      volumeuniq= provideOrderFormRepository.selectBycountvolume(provideCommodity.getId());
						       vo=provideOrderFormRepository.selectBycountTotalOrder(provideCommodity.getId());
						      sumvolume+=vo;  
						      sumPrice+=shangpinPrice;
						      volume1+=volumeuniq;
							}
							operatorUserRo.setTransactionAmount(sumPrice);
							operatorUserRo.setRelease(volume1);
							//成交率(成交量/生成订单总数)   
							//成交量：成交笔数完成             成交是以最终客户确认，订单全部关闭为准  
							//生成订单总数：平台生成的订单量（订单总数）
							String str_i=Integer.toString(volume);
							String str_j=Integer.toString(sumvolume);
							if(volume==0||volume==0&&sumvolume==0){
								operatorUserRo.setClosing(0.0);
							}else{
								operatorUserRo.setClosing(Double.valueOf(str_i)/Double.valueOf(str_j));
							}
					  
				 }
				
			}
	     }
		 if(volumesymbol!=null&&volumesymbol.equals("大于")&&volume!=null){
			 for (int i = 0; i < listI.size(); i++) {
				 operatorUserRo=listI.get(i);
		    	  if(operatorUserRo.getRelease()!=null){
		    		  if(volume<operatorUserRo.getRelease()){
		    			  if(lists.size()>0){
		    				  OperatorUserRo  operatorUserRo1=new OperatorUserRo();
		    				 for (int j = 0; j < lists.size(); j++) {
		    					 operatorUserRo1=lists.get(i);
		    					  if(volume<operatorUserRo1.getRelease()){
		    						  lists.add(operatorUserRo);
		    					  }else{
		    						  lists.remove(i);
		    					  }
							}
		    			  }else{
		    				  lists.add(operatorUserRo);
		    			  }
		    		  }
		    	  }
			   }
			  }else if(volumesymbol!=null&&volumesymbol.equals("小于")&&volume!=null){
				  for (int i = 0; i < listI.size(); i++) {
						 operatorUserRo=listI.get(i);
				    	  if(operatorUserRo.getRelease()!=null){
				    		  if(volume>operatorUserRo.getRelease()){
				    			  if(lists.size()>0){
				    				  OperatorUserRo  operatorUserRo1=new OperatorUserRo();
				    				 for (int j = 0; j < lists.size(); j++) {
				    					 operatorUserRo1=lists.get(i);
				    					  if(volume>operatorUserRo1.getRelease()){
				    						  lists.add(operatorUserRo);
				    					  }else{
				    						  lists.remove(i);
				    					  }
									}
				    			  }else{
				    				  lists.add(operatorUserRo);
				    			  }
				    		  }
				    	  }
					   }
			  }else if(volumesymbol!=null&&volumesymbol.equals("等于")&&volume!=null){
				  for (int i = 0; i < listI.size(); i++) {
						 operatorUserRo=listI.get(i);
				    	  if(operatorUserRo.getRelease()!=null){
				    		  if(volume==operatorUserRo.getRelease()){
				    			  if(lists.size()>0){
				    				  OperatorUserRo  operatorUserRo1=new OperatorUserRo();
				    				 for (int j = 0; j < lists.size(); j++) {
				    					 operatorUserRo1=lists.get(i);
				    					  if(volume==operatorUserRo1.getRelease()){
				    						  lists.add(operatorUserRo);
				    					  }else{
				    						  lists.remove(i);
				    					  }
									}
				    			  }else{
				    				  lists.add(operatorUserRo);
				    			  }
				    		  }
				    	  }
					   }
			   }else if(clinchadealAmountsymbol!=null&&clinchadealAmountsymbol.equals("大于")&&clinchadealAmount!=null){
				   for (int i = 0; i < listI.size(); i++) {
						 operatorUserRo=listI.get(i);
				    	  if(operatorUserRo.getTransactionAmount()!=null){
				    		  if(clinchadealAmount<operatorUserRo.getTransactionAmount()){
				    			  if(lists.size()>0){
				    				  OperatorUserRo  operatorUserRo1=new OperatorUserRo();
				    				 for (int j = 0; j < lists.size(); j++) {
				    					 operatorUserRo1=lists.get(i);
				    					  if(clinchadealAmount<operatorUserRo1.getTransactionAmount()){
				    						  lists.add(operatorUserRo);
				    					  }else{
				    						  lists.remove(i);
				    					  }
									}
				    			  }else{
				    				  lists.add(operatorUserRo);
				    			  }
				    		  }
				    	  }
					   }
			   } else if(clinchadealAmountsymbol!=null&&clinchadealAmountsymbol.equals("小于")&&clinchadealAmount!=null){
				   for (int i = 0; i < listI.size(); i++) {
						 operatorUserRo=listI.get(i);
				    	  if(operatorUserRo.getTransactionAmount()!=null){
				    		  if(clinchadealAmount>operatorUserRo.getTransactionAmount()){
				    			  if(lists.size()>0){
				    				  OperatorUserRo  operatorUserRo1=new OperatorUserRo();
				    				 for (int j = 0; j < lists.size(); j++) {
				    					 operatorUserRo1=lists.get(i);
				    					  if(clinchadealAmount>operatorUserRo1.getTransactionAmount()){
				    						  lists.add(operatorUserRo);
				    					  }else{
				    						  lists.remove(i);
				    					  }
									}
				    			  }else{
				    				  lists.add(operatorUserRo);
				    			  }
				    		  }
				    	  }
					   }
			   }else if(clinchadealAmountsymbol!=null&&clinchadealAmountsymbol.equals("等于")&&clinchadealAmount!=null){
				   for (int i = 0; i < listI.size(); i++) {
						 operatorUserRo=listI.get(i);
				    	  if(operatorUserRo.getTransactionAmount()!=null){
				    		  if(clinchadealAmount==operatorUserRo.getTransactionAmount()){
				    			  if(lists.size()>0){
				    				  OperatorUserRo  operatorUserRo1=new OperatorUserRo();
				    				 for (int j = 0; j < lists.size(); j++) {
				    					 operatorUserRo1=lists.get(i);
				    					  if(clinchadealAmount==operatorUserRo1.getTransactionAmount()){
				    						  lists.add(operatorUserRo);
				    					  }else{
				    						  lists.remove(i);
				    					  }
									}
				    			  }else{
				    				  lists.add(operatorUserRo);
				    			  }
				    		  }
				    	  }
					   }
				   
			   }else if(closingsymbol!=null&&closingsymbol.equals("大于")&&closing!=null){
				   for (int i = 0; i < listI.size(); i++) {
						 operatorUserRo=listI.get(i);
				    	  if(operatorUserRo.getClosing()!=null){
				    		  if(closing<operatorUserRo.getClosing()){
				    			  if(lists.size()>0){
				    				  OperatorUserRo  operatorUserRo1=new OperatorUserRo();
				    				 for (int j = 0; j < lists.size(); j++) {
				    					 operatorUserRo1=lists.get(i);
				    					  if(closing<operatorUserRo1.getClosing()){
				    						  lists.add(operatorUserRo);
				    					  }else{
				    						  lists.remove(i);
				    					  }
									}
				    			  }else{
				    				  lists.add(operatorUserRo);
				    			  }
				    		  }
				    	  }
					   }
				   
			   }else if(closingsymbol!=null&&closingsymbol.equals("小于")&&closing!=null){
				   for (int i = 0; i < listI.size(); i++) {
						 operatorUserRo=listI.get(i);
				    	  if(operatorUserRo.getClosing()!=null){
				    		  if(closing>operatorUserRo.getClosing()){
				    			  if(lists.size()>0){
				    				  OperatorUserRo  operatorUserRo1=new OperatorUserRo();
				    				 for (int j = 0; j < lists.size(); j++) {
				    					 operatorUserRo1=lists.get(i);
				    					  if(closing>operatorUserRo1.getClosing()){
				    						  lists.add(operatorUserRo);
				    					  }else{
				    						  lists.remove(i);
				    					  }
									}
				    			  }else{
				    				  lists.add(operatorUserRo);
				    			  }
				    		  }
				    	  }
					   }
				   
			   }else if(closingsymbol!=null&&closingsymbol.equals("等于")&&closing!=null){
				   for (int i = 0; i < listI.size(); i++) {
						 operatorUserRo=listI.get(i);
				    	  if(operatorUserRo.getClosing()!=null){
				    		  if(closing==operatorUserRo.getClosing()){
				    			  if(lists.size()>0){
				    				  OperatorUserRo  operatorUserRo1=new OperatorUserRo();
				    				 for (int j = 0; j < lists.size(); j++) {
				    					 operatorUserRo1=lists.get(i);
				    					  if(closing==operatorUserRo1.getClosing()){
				    						  lists.add(operatorUserRo);
				    					  }else{
				    						  lists.remove(i);
				    					  }
									}
				    			  }else{
				    				  lists.add(operatorUserRo);
				    			  }
				    		  }
				    	  }
					   }
			   }
		    map.put("result", listI);
			return  map;
	
   }
	
	
}
